using System;
using System.Collections;
using System.ComponentModel;
using System.Web.UI;
using Sedna.Core.Controller;
using Sedna.Core.Controls.Support;
using Sedna.Core.DataBinding;
using Sedna.Core.Tiles.Contents;
using DataBinder = Sedna.Core.DataBinding.DataBinder;

namespace Sedna.Core.Controls
{
	public class LiteralControl : System.Web.UI.LiteralControl, IContent, IConfigurationAware,  ILabelledControl, IControlDataLoadAware
	{
		private IConfiguration configuration;
		private string configurationId = "configuration.default.LiteralControl";
		private string textKey;

		protected IDataBinder binder;


		private String contentPlaceHolderId;

		public String ContentPlaceHolderId
		{
			get { return contentPlaceHolderId;}
			set { contentPlaceHolderId = value;}
		}

		public IConfiguration Configuration
		{
			get { return configuration; }
			set { this.configuration = value; }
		}

		[Bindable(true), Category("Sedna"), DefaultValue("configuration.default.LiteralControl")]
		public string ConfigurationId
		{
			get { return configurationId; }
			set { this.configurationId = value; }
		}

		public string TextKey
		{
			get { return textKey; }
			set { textKey = value; }
		}

		public LiteralControl()
			:base()
		{
//			this.CssClass = "workarea";
		}

		public LiteralControl(string id, string textKey , string text)
			:this()
		{
			this.ID = id;
			this.textKey = textKey;
			this.Text = text;
		}

		public LiteralControl(IConfiguration configuration)
		{ 
			this.configuration = configuration;
			this.configurationId = configuration.ObjectName;
			BuildControlFromConfiguration();
		}

		protected override void OnInit(EventArgs e)
		{
			if (configuration == null)
			{
				configuration = (IConfiguration)SpringContextFactory.GetContext().GetObject(configurationId);	
			}
//			ControlUtil.SetupClientAttributes(this, configuration);
//			ControlUtil.SetupValidators(this, configuration);
			Build();
			binder=new DataBinder(this, configuration, ExecutionContext.GetCurrent(ClientID));
			base.OnInit (e);
		}

		protected void BuildControlFromConfiguration()
		{
			this.ID = configuration.Id;
			this.TextKey = configuration.TextKey;
		}

		protected void Build()
		{
			this.Visible = configuration.IsVisibleForPrincipal();
//			this.Enabled = configuration.IsEnabledForPrincipal();
//			this.Text = configuration.GetLocalizedPostfixLabel(textKey, this.Text);
		}

		protected override void OnLoad(EventArgs e)
		{
			base.OnLoad(e);
			IDictionary executionContext = ExecutionContext.GetCurrent(ClientID);
			Unbind();
			ControlUtil.ExecuteOnLoad(executionContext, Context, Configuration, this.ClientID);
		}

		public void OnDataLoad()
		{
			IDictionary executionContext = ExecutionContext.GetCurrent(ClientID);
			ControlUtil.ExecuteOnDataLoad(executionContext, Context, Configuration, this.ClientID);
		}

		protected override void OnPreRender(EventArgs e)
		{
			base.OnPreRender(e);
			IDictionary executionContext = ExecutionContext.GetCurrent(ClientID);
			ControlUtil.ExecuteOnPreRender(executionContext,Context, Configuration, this.ClientID);
			Bind();
		}

		private void Bind()
		{
			binder.Bind(new string[]{"Text", "TextKey"});
			Text=Configuration.GetLocalizedPostfixLabel(TextKey, Text);
		}

		private void Unbind()
		{
			binder.Unbind(new string[]{"Text", "TextKey"});	
		}
		
		
/*
		private void LoadState()
		{
			IDictionary executionContext = ExecutionContext.GetCurrent(ClientID);
			
			IActionExecutor actionExecutor = (IActionExecutor)SpringContextFactory.GetContext().GetObject("runtime.ActionExecutor");
			if(executionContext.Contains("controlState"))
			{
				executionContext.Remove("controlState");
			}
			actionExecutor.ExecuteAction(Context, executionContext, configuration, "loadState");
			if(!executionContext.Contains("controlState")) return;

			IDictionary state = (IDictionary)executionContext["controlState"];
			this.TextKey = (string)state["textKey"];
			this.Text = (string)state["text"];
		}

		public void FillControlData(string controlId,IDictionary ht){}
		
*/
	}
}
